package com.feonix.common.view.tree;

import com.feonix.common.permission.IPermissionCheckor;
import com.feonix.common.util.JsonUtil;
import com.feonix.common.util.StringUtil;
import java.util.ArrayList;
import java.util.List;

public class Tree {
	private List<TreeNode> treeNodes = new ArrayList<TreeNode>();

	/**
	 * 树节点id
	 */
	private String idQueryName = "treeNodeId";

	/**
	 * 树节点名称
	 */
	private String nodeQueryName = "treeNodeName";

	/**
	 * 获得一个树实例
	 * 
	 * @return
	 */
	public static Tree getInstance() {
		return new Tree();
	}

	/**
	 * 获得一个树实例
	 * 
	 * @param idQueryName
	 *            节点id在url上的参数名称
	 * @param nodeQueryName
	 *            节点name在url上的参数名称
	 * @return
	 */
	public static Tree getInstance(String idQueryName, String nodeQueryName) {
		Tree tree = new Tree();
		if (StringUtil.isNotEmpty(idQueryName)) {
			tree.setIdQueryName(idQueryName);
		}
		if (StringUtil.isNotEmpty(nodeQueryName)) {
			tree.setNodeQueryName(nodeQueryName);
		}
		return tree;
	}

	/**
	 * 组织ztree
	 * 
	 * @return
	 */
	public String parse() {
		return JsonUtil.objectToString(this.treeNodes);
	}

	/**
	 * 组织ztree
	 * 
	 * @param paramList
	 * @return
	 */
	public static String parse(List<TreeNode> treeNodes) {
		ArrayList<TreeNode> treeNodeList = new ArrayList<TreeNode>();
		if (treeNodes != null) {
			for (TreeNode treeNode : treeNodes) {
				if (treeNode.hasRight()) {
					treeNodeList.add(treeNode);
				}
			}
		}
		return JsonUtil.objectToString(treeNodeList);
	}

	/**
	 * 
	 * @param paramList
	 * @param paramIPermissionCheckor
	 * @return
	 */
	public static String parse(List<TreeNode> treeNodes, IPermissionCheckor<TreeNode> checkor) {
		ArrayList<TreeNode> treeNodeList = new ArrayList<TreeNode>();
		if (treeNodes != null) {
			for (TreeNode treeNode : treeNodes) {
				if (checkor.check(treeNode)) {
					treeNodeList.add(treeNode);
				}
			}
		}
		return JsonUtil.objectToString(treeNodeList);
	}

	/**
	 * 增加节点
	 * 
	 * @param treeNode
	 *            节点（通过TreeNode.getInstance() 获取）
	 */
	public void addNode(TreeNode treeNode) {
		if ((treeNode == null) || (!treeNode.hasRight())) {
			return;
		}
		treeNode.addParam(this.idQueryName, treeNode.getId());
		treeNode.addParam(this.nodeQueryName, treeNode.getName());
		this.treeNodes.add(treeNode);
	}

	/**
	 * 增加节点
	 * 
	 * @param treeNode
	 *            节点（通过TreeNode.getInstance() 获取）
	 * @param checkor
	 */
	public void addNode(TreeNode treeNode, IPermissionCheckor<TreeNode> checkor) {
		if (treeNode == null) {
			return;
		}
		treeNode.addParam(this.idQueryName, treeNode.getId());
		treeNode.addParam(this.nodeQueryName, treeNode.getName());
		if (checkor.check(treeNode)) {
			this.treeNodes.add(treeNode);
		}
	}

	private void setIdQueryName(String idQueryName) {
		this.idQueryName = idQueryName;
	}

	private void setNodeQueryName(String nodeQueryName) {
		this.nodeQueryName = nodeQueryName;
	}
}
